#include "IO.h"

//-----------------------------------充电-----------------------------------
void Charging_init(void){
    gpio_pad_select_gpio(Chg_IO);
	gpio_set_direction(Chg_IO, GPIO_MODE_INPUT);
    gpio_pad_select_gpio(ChgFull_IO);
	gpio_set_direction(ChgFull_IO, GPIO_MODE_INPUT);
}

//-----------------------------------按键-----------------------------------
void Btu_init(void){
    gpio_pad_select_gpio(Btu_IO);
	gpio_set_direction(Btu_IO, GPIO_MODE_INPUT);
}

//-----------------------------------电源开关-----------------------------------
void Power_init(void){
    gpio_pad_select_gpio(Pwr_IO);
	gpio_set_direction(Pwr_IO, GPIO_MODE_OUTPUT);
	Pwr_OFF;//默认电源关闭
}


//-----------------------------------磁阻传感器-----------------------------------
void Pluse_init(void){
    gpio_pad_select_gpio(Pluse_IO);
	gpio_set_direction(Pluse_IO, GPIO_MODE_INPUT);
}

uint8_t PrintEnd=0;//打印结束标志
uint32_t INPulse_count=0;//滚轮滚动步数
static const uint8_t num_byte=39;
static void IRAM_ATTR Pluse_isr_handler(void* arg){
	if(StartPrintFlag){//开启打印
		INPulse_count++;
		if(INPulse_count>=((data_length/39)+105)){			//打印完成
			INPulse_count = 0;
			PrintEnd = 1;
			Beep_set(100);
			if(Print_ContinueFlag){							//判断是否连续打印
				esp_tim_run_flg.PrintContinue = 1;
			}else{
				esp_tim_run_flg.PrintContinue = 0;			//没意义，为了看着舒服
				StartPrintFlag = 0; 						//关闭打印
				gpio_intr_disable(Pluse_IO);				//关闭外部引脚中断
				uint8_t resp_data[4] = {0x6A,0xA6,0x3,0xC};	//打印结束指令
				SendToApp(sizeof(resp_data),resp_data);
				LED5_OFF;								   	//打印灯灭
			}
		}
		if(INPulse_count>=105)//滑动脉冲数计数值大于100的时候才开始打印，这个105刚好是Pen的机身侧边起点开始打印
			Print_data(&RxBuf[num_byte*(INPulse_count-105)+7]);//需要除去包头长度7
	}else INPulse_count = 0;
}

void IsrPluse_init(void){
	gpio_config_t io_conf;
	io_conf.intr_type = GPIO_PIN_INTR_POSEDGE;//GPIO中断类型
	io_conf.pin_bit_mask = (1ULL<<Pluse_IO);  //设置位掩码，每个位映射到一个GPIO
	io_conf.mode = GPIO_MODE_INPUT;			  //设置输入/输出模式
	io_conf.pull_up_en = 1;					  //enable pull-up mode
	gpio_config(&io_conf); 

	gpio_install_isr_service(0);//服务允许每个GPIO中断处理程序
	gpio_isr_handler_add(Pluse_IO, Pluse_isr_handler, (void*)Pluse_IO);
}

//-----------------------------------LED灯-----------------------------------
void Led_Init(void){
    gpio_pad_select_gpio(LED_1);
	gpio_set_direction(LED_1, GPIO_MODE_OUTPUT);
	gpio_pad_select_gpio(LED_2);
	gpio_set_direction(LED_2, GPIO_MODE_OUTPUT);
	gpio_pad_select_gpio(LED_3);
	gpio_set_direction(LED_3, GPIO_MODE_OUTPUT);
	gpio_pad_select_gpio(LED_4);
	gpio_set_direction(LED_4, GPIO_MODE_OUTPUT);
	gpio_pad_select_gpio(LED_5);
	gpio_set_direction(LED_5, GPIO_MODE_OUTPUT);
	Led_set(0);//开机灯光默认全部关闭
	LedPm.number = 0;//闪烁灯默认全无
}

void Led_McuOnSta(void)//led开机初始运行状态
{
	Led_set(0);//灯光全灭
	Led_set(2);//开始闪烁
	LED2_TWK_ON;LED3_TWK_ON;//开启闪烁状态
	LED1_ON;LED2_ON;LED3_ON;LED4_ON;//开启状态
}


void Led_set(int sta){	//sta 0:灭灯  sta 1:亮灯  sta 2:闪灯
	
	if(sta==0){
		LED1_Ctl(1);
		LED2_Ctl(1);
		LED3_Ctl(1);
		LED4_Ctl(1);
		LED5_Ctl(1);
		LedPm.number = 0;
		LedPm.twinkle = 0;
	}
	else if(sta==1){
		LED1_Ctl(0);
		LED2_Ctl(0);
		LED3_Ctl(0);
		LED4_Ctl(0);
		LED5_Ctl(0);
		LedPm.number = 0;
		LedPm.twinkle = 0;
	}
	else if(sta==2){
		LedPm.twinkle = 1;
		LedPm.runcount = 5000;//0.5秒闪烁
	}
} 

//-----------------------------------蜂鸣器-----------------------------------
void Beep_Init(void){
    gpio_pad_select_gpio(Beep_IO);
	gpio_set_direction(Beep_IO, GPIO_MODE_OUTPUT);
}

void Beep_set(uint16_t tim){//单位(ms)
	if(!BePm.runFlag){
		BePm.runFlag = 1;//运行蜂鸣器
		BePm.expandCnt = 1;//单次运行
		BePm.count = tim*10;//单位：0.1ms
		BePm.countSave = BePm.count;
	}
}

void BeepExp_set(uint16_t tim,uint8_t cnt){//单位(ms)
	if(!BePm.runFlag){
		BePm.runFlag = 1;//运行蜂鸣器
		BePm.expandCnt = cnt;//循环运行次数
		BePm.count = tim*10;//单位：0.1ms
		BePm.countSave = BePm.count;
	}
}

//-----------------------------------IO口初始化-----------------------------------
void IO_Init(void){
	Btu_init();
	Charging_init();
	Power_init();
	Pluse_init();
	IsrPluse_init();
	Led_Init();
	Beep_Init();
}


